var iconSize = new OpenLayers.Size(21, 25);
var iconOffset = new OpenLayers.Pixel(-(iconSize.w / 2), -iconSize.h);
var icon = new OpenLayers.Icon("../images/marker.png", iconSize, iconOffset);
var blue_icon = new OpenLayers.Icon("../images/blue_marker.png", iconSize, iconOffset);
var pink_icon = new OpenLayers.Icon("../images/pink_marker.png", iconSize, iconOffset);
var bounds = new OpenLayers.Bounds();
var fromProjection = new OpenLayers.Projection("EPSG:4326");
var toProjection   = new OpenLayers.Projection("EPSG:900913");
var lyrMarkers = new OpenLayers.Layer.Markers("Markers");
var zoom, center, currentPopup, map;
var popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
    "autoSize": true,
    "minSize": new OpenLayers.Size(300, 50),
    "maxSize": new OpenLayers.Size(500, 300),
    "keepInMap": true
});


function addMarker(lng, lat, info, color) {
    var pt = new OpenLayers.LonLat(lat, lng).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
    bounds.extend(pt);
    var feature = new OpenLayers.Feature(lyrMarkers, pt);
    feature.closeBox = true;
    feature.popupClass = popupClass;
    feature.data.popupContentHTML = info ;
    feature.data.overflow = "auto";
    var marker;
    var opcion = color.trim();
    switch(opcion)
    {
    case "Femenino":
        marker = new OpenLayers.Marker(pt, pink_icon.clone());
    break;
    case "Masculino":
        marker = new OpenLayers.Marker(pt, blue_icon.clone());
    break;
    default:
        marker = new OpenLayers.Marker(pt, icon.clone());
    }
    var markerClick = function(evt) {
        if (currentPopup != null && currentPopup.visible()) {
            currentPopup.hide();
        }
        if (this.popup == null) {
            this.popup = this.createPopup(this.closeBox);
            map.addPopup(this.popup);
            this.popup.show();
        } else {
            this.popup.toggle();
        }
        currentPopup = this.popup;
        OpenLayers.Event.stop(evt);
    };
    marker.events.register("mousedown", feature, markerClick);
    lyrMarkers.addMarker(marker);
}


function dibujarPuntos(punto, info, color){
    map.addControl(new OpenLayers.Control.DragPan());
    var lyrOsm = new OpenLayers.Layer.OSM();
    map.addLayer(lyrOsm);
    map.addLayer(lyrMarkers);
    //add marker on given coordinates
    var aux,aux_2,lon,lat;
    aux = punto.substring(1, punto.length-1);
    aux = aux.split("POINT (");
    aux_2 = aux[1].split(" ")
    lat = aux_2[0]
    lon = aux_2[1]
    addMarker(lat,lon, info, color);
    center = bounds.getCenterLonLat();
    map.setCenter(center, map.getZoomForExtent(bounds) - 1);
    zoom = map.getZoom();
}

function clearMarkers(){
   lyrMarkers.clearMarkers();
}

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
                defaultHandlerOptions: {
                    'single': true,
                    'double': false,
                    'pixelTolerance': 0,
                    'stopSingle': false,
                    'stopDouble': false
                },

                initialize: function(options) {
                    this.handlerOptions = OpenLayers.Util.extend(
                        {}, this.defaultHandlerOptions
                    );
                    OpenLayers.Control.prototype.initialize.apply(
                        this, arguments
                    );
                    this.handler = new OpenLayers.Handler.Click(
                        this, {
                            'click': this.trigger
                        }, this.handlerOptions
                    );
                },

                trigger: function(e) {
                    var lonlat = map.getLonLatFromPixel(e.xy);
                    lonlat1= new OpenLayers.LonLat(lonlat.lon,lonlat.lat).transform(toProjection,fromProjection);
                    ${remoteFunction(
                        controller: 'salaSituacional',
                        action: 'getDistanciaPuntos',
                        onComplete: 'generarEstadisticas()',
                        asynchronous: 'false',
                        update: 'pacientes',
                        params: '\'coordenadas=\' + lonlat1')}
                }
});